/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.pages;

import com.google.inject.Inject;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.services.DeckParser;
import nl.xs4all.dylanschell.deckprinter.services.DeckService;
import nl.xs4all.dylanschell.deckprinter.services.ImageService;
import nl.xs4all.dylanschell.deckprinter.services.SetService;
import nl.xs4all.dylanschell.deckprinter.transform.RemoveUnknownCards;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.lang.Bytes;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class UploadDeckPage extends BasePage
{
    private final FileUploadField file;

    @Inject
    public DeckParser parser;

    @Inject
    public ImageService imageService;

    @Inject
    public SetService setService;

    @Inject
    public DeckService deckService;

    //    private DeckDescriptor deck;
    private String url;

    public UploadDeckPage()
    {
        // create a feedback panel
        final Component feedback = new FeedbackPanel("feedback").setOutputMarkupPlaceholderTag(true);
        add(feedback);

        final WebMarkupContainer actions = new WebMarkupContainer("actions");
        actions.setOutputMarkupId(true);
        actions.setOutputMarkupPlaceholderTag(true);
        add(actions);
        Link home = new BookmarkablePageLink("home", getApplication().getHomePage());
        actions.add(home);

        Form<?> form = new Form<Void>("form")
        {
            @Override
            protected void onSubmit()
            {
                FileUpload upload = file.getFileUpload();
                if (upload != null)
                {
                    info("File-Name: " + upload.getClientFileName() + " File-Size: " +
                            Bytes.bytes(upload.getSize()).toString());
                }

                try
                {
                    DeckDescriptor deck = parser.parseDeck(upload.getInputStream(), upload.getClientFileName());
                    File deckFile = deckService.saveDeck(deck);
                    setResponsePage(new DeckViewPage(deckFile));
                } catch (IOException e)
                {
                    error("Unable to load deck: " + e.getMessage());
                }
            }
        };
        form.setMaxSize(Bytes.megabytes(1));
        add(form);
        form.add(file = new FileUploadField("file"));

        // create the ajax button used to submit the form
        form.add(new AjaxButton("ajaxSubmit")
        {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                target.addComponent(feedback);
                target.addComponent(actions);
            }

        });

        Form<?> form2 = new Form<Void>("form2")
        {
            @Override
            protected void onSubmit()
            {

                try
                {
                    URL url = new URL(getUrl());
                    String name = getUrl();
                    int slash = name.lastIndexOf("/");

                    DeckDescriptor deck = parser.parseDeck(url, name.substring(slash + 1));
                    File deckFile = deckService.saveDeck(deck);
                    setResponsePage(new DeckViewPage(deckFile));
                } catch (IOException e)
                {
                    error("Unable to load deck: " + e.getMessage());
                }
            }
        };
        add(form2);
        form2.add(new TextField("url", new PropertyModel<String>(this, "url")));

        // create the ajax button used to submit the form
        form2.add(new AjaxButton("ajaxSubmit")
        {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                target.addComponent(feedback);
                target.addComponent(actions);
            }

        });

    }

    @Override
    public String getTitle()
    {
        return "Upload Deck";
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }
}
